package com.example.xianji.activity;

import com.example.xianji.Contant;
import com.example.xianji.R;
import com.example.xianji.adapter.FuWu_Adapter;
import com.example.xianji.fragment.ChooseDaTingFragment;
import com.example.xianji.fragment.HomeFragment;
import com.example.xianji.fragment.MyXianJiFragment;
import com.example.xianji.fragment.XuQiuFragment;
import com.example.xianji.eventbus.DianJi_EventBus;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/*
 * 主页面
 */
public class MainActivity extends FragmentActivity {
	/** 首页Fragment **/
	private HomeFragment mHomeFragment;
	/** 需求大厅Fragment **/
	private XuQiuFragment mXuQiuFragment;
	/** 选型大厅Fragment **/
	private ChooseDaTingFragment mChooseDaTingFragment;
	/** 我的后台Fragment **/
	private MyXianJiFragment mMyXianJiFragment;
	private ImageView ivMain, ivXuQiu, ivChoose, ivMyXianJi;
	private TextView tvMain, tvXuQiu, tvChoose, tvMyXianJi;
	private ImageView[] image;
	private TextView[] text;
	private Fragment[] mFragment;
	private int mClickedIndex;
	private int currentIndex;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		}

		setContentView(R.layout.activity_main);
		if(!EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().register(this);
		}

		FragmentTransaction transaction = getSupportFragmentManager()
				.beginTransaction();

		mHomeFragment = new HomeFragment();
		mXuQiuFragment = new XuQiuFragment();
		mChooseDaTingFragment = new ChooseDaTingFragment();
		mMyXianJiFragment = new MyXianJiFragment();

		mFragment = new Fragment[] {mHomeFragment, mXuQiuFragment,
				mChooseDaTingFragment, mMyXianJiFragment};

		ivMain = (ImageView) findViewById(R.id.iv_main_activity_main);
		ivXuQiu = (ImageView) findViewById(R.id.iv_hot_requre_fragment_home);
		ivChoose = (ImageView) findViewById(R.id.iv_choose_activity_main);
		ivMyXianJi = (ImageView) findViewById(R.id.iv_my_xianji_activity_main);
		image = new ImageView[] {ivMain, ivXuQiu, ivChoose,ivMyXianJi};

		tvMain = (TextView) findViewById(R.id.tv_main_activity_main);
		tvXuQiu = (TextView) findViewById(R.id.tv_xuqiu_activity_main);
		tvChoose = (TextView) findViewById(R.id.tv_choose_activity_main);
		tvMyXianJi = (TextView) findViewById(R.id.tv_my_xianji_activity_main);
		text = new TextView[] {tvMain, tvXuQiu, tvChoose, tvMyXianJi};
		transaction.add(R.id.frame_layout, mHomeFragment).show(mHomeFragment).commit();
		ivMain.setSelected(true);
		tvMain.setTextColor(getResources().getColor(R.color.blue22));
		currentIndex = 0;
	}


	public void onTabClicked(View view) {
		if (view.getId() == R.id.rl_main_activity_main) {
			mClickedIndex = 0;
		} else if (view.getId() == R.id.rl_xuqiu_activity_main) {
			mClickedIndex = 1;
		} else if (view.getId() == R.id.rl_choose_activity_main) {
			mClickedIndex = 2;
			// 选型大厅
			Contant.choose_dating_back = 1;
		} else if (view.getId() == R.id.rl_my_xianji_activity_main) {
			mClickedIndex = 3;
		}
		if (currentIndex != mClickedIndex) {
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			if (!mFragment[mClickedIndex].isAdded()) {
				transaction.add(R.id.frame_layout, mFragment[mClickedIndex])
						.hide(mFragment[currentIndex]).show(mFragment[mClickedIndex])
						.commit();
			} else {
				transaction.hide(mFragment[currentIndex])
						.show(mFragment[mClickedIndex]).commit();
			}
			image[mClickedIndex].setSelected(true);
			text[mClickedIndex].setTextColor(getResources().getColor(R.color.blue22));
			image[currentIndex].setSelected(false);
			text[currentIndex].setTextColor(getResources().getColor(R.color.black6));
			currentIndex = mClickedIndex;
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
	public void onEventMainThread(DianJi_EventBus event) {
		if (event.find_fuwu_flag) {
			mClickedIndex = 2;
		} else if (event.find_xiangmu_flag) {
			mClickedIndex = 1;
		}
		if (currentIndex != mClickedIndex) {
			FragmentTransaction transaction = getSupportFragmentManager()
					.beginTransaction();
			/*
			 * 大致意思是说我使用的commit方法是在Activity的onSaveInstanceState()之后调用的，
			 * 这样会出错，因为onSaveInstanceState方法是在该Activity即将被销毁前调用，来保存
			 * Activity数据的，如果在保存玩状态后再给它添加Fragment就会出错。解决办法就是
			 * 把commit()方法替换成commitAllowingStateLoss()就行了，其效果是一样的。
			 */
			if (!mFragment[mClickedIndex].isAdded()) {
				transaction.add(R.id.frame_layout, mFragment[mClickedIndex])
						.hide(mFragment[currentIndex]).show(mFragment[mClickedIndex])
						.commitAllowingStateLoss();
			} else {
				transaction.hide(mFragment[currentIndex])
						.show(mFragment[mClickedIndex]).commitAllowingStateLoss();
			}
			image[mClickedIndex].setSelected(true);
			text[mClickedIndex].setTextColor(getResources().getColor(R.color.blue22));
			image[currentIndex].setSelected(false);
			text[currentIndex].setTextColor(getResources().getColor(R.color.black6));
			currentIndex = mClickedIndex;
		}
	}

	@Override
	protected void onRestart() {
		super.onRestart();

	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		FuWu_Adapter.AnimateFirstDisplayListener.displayedImages.clear();
		EventBus.getDefault().unregister(this);
	}
}
